#include "stdafx.h"
#include "Settings.h"
#include "SettingsComponent.h"

juce_ImplementSingleton(Settings);

class SettingsDialog : public DialogWindow
{
public:
	SettingsDialog() : DialogWindow(T("Komodo Drop"), Colours::white, true)
	{	
		setContentComponent(new SettingsComponent(), true, true);
		centreWithSize(getWidth(), getHeight());
	}

	~SettingsDialog()
	{
		exitModalState(0);
	}

	void closeButtonPressed()
	{
		delete this;
	}
};

Settings::Settings()
{
	props = PropertiesFile::createDefaultAppPropertiesFile(T("Komodo Drop"), T(".xml"), T("Komodo Drop"), false, 1000, PropertiesFile::storeAsXML);

	password        = props->getValue(T("password"));
	email           = props->getValue(T("email"));
	silentUpload    = props->getBoolValue(T("silentUpload"));
	lastCateory		= props->getIntValue(T("lastCategory"), -1);
	lastSubCategory = props->getIntValue(T("lastSubCategory"), -1);
	lastMode        = props->getIntValue(T("lastMode"), -1);
	lastAlbum       = props->getIntValue(T("lastAlbum"), -1);
	checkForDupes   = props->getBoolValue(T("checkForDupes"), true);
	uploadThreads   = props->getIntValue(T("uploadThreads"), 2);
	x               = props->getIntValue(T("x"), -1);
	y				= props->getIntValue(T("y"), -1);
	sort			= props->getIntValue(T("sort"), 1);
	ascending		= props->getBoolValue(T("ascending"), true);
    public_			= props->getBoolValue(T("public_"), true);
    filenames		= props->getBoolValue(T("filenames"), false);
    comments		= props->getBoolValue(T("comments"), true);
    linking			= props->getBoolValue(T("linking"), true);
    exif			= props->getBoolValue(T("exif"), true);
    aesyshare		= props->getBoolValue(T("aesyshare"), true);
    originals		= props->getBoolValue(T("originals"), true);
    header			= props->getBoolValue(T("header"), false);
    large			= props->getBoolValue(T("large"), true);
    clean			= props->getBoolValue(T("clean"), false);
    protect			= props->getBoolValue(T("protect"), false);
    watermark		= props->getBoolValue(T("watermark"), false);
    smugsearch		= props->getBoolValue(T("smugsearch"), true);
    worldsearch		= props->getBoolValue(T("worldsearch"), true);
    friendedit		= props->getBoolValue(T("friendedit"), false);
    familyedit		= props->getBoolValue(T("familyedit"), false);
	openBrowser     = props->getBoolValue(T("openBrowser"), true);
}

Settings::~Settings()
{
	save();
	delete props;
}

void Settings::save()
{
	props->setValue(T("password"), password);
	props->setValue(T("email"), email);
	props->setValue(T("silentUpload"), silentUpload);
	props->setValue(T("lastCategory"), lastCateory);
	props->setValue(T("lastSubCategory"), lastSubCategory);
	props->setValue(T("lastMode"), lastMode);
	props->setValue(T("lastAlbum"), lastAlbum);
	props->setValue(T("checkForDupes"), checkForDupes);
	props->setValue(T("uploadThreads"), uploadThreads);
	props->setValue(T("x"), x);
	props->setValue(T("y"), y);
	props->setValue(T("sort"), sort);
	props->setValue(T("ascending"), ascending);
	props->setValue(T("public_"), public_);
	props->setValue(T("filenames"), filenames);
	props->setValue(T("comments"), comments);
	props->setValue(T("linking"), linking);
	props->setValue(T("exif"), exif);
	props->setValue(T("aesyshare"), aesyshare);
	props->setValue(T("originals"), originals);
	props->setValue(T("header"), header);
	props->setValue(T("large"), large);
	props->setValue(T("clean"), clean);
	props->setValue(T("protect"), protect);
	props->setValue(T("watermark"), watermark);
	props->setValue(T("smugsearch"), smugsearch);
	props->setValue(T("worldsearch"), worldsearch);
    props->setValue(T("friendedit"), friendedit);
    props->setValue(T("familyedit"), familyedit);
	props->setValue(T("openBrowser"), openBrowser);

	props->saveIfNeeded();
}

void Settings::showSettingsDialog()
{
	SettingsDialog* d = new SettingsDialog();
	d->runModalLoop();	
}
